<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/11/23
 * Time: 14:00
 */
namespace api\controllers;


use Yii;
use common\services\CryptoService;
use common\services\config\Config;
use common\services\ToolStringService;

class CryptoController extends BaseController
{
    /**
     * 中文转拼音
     * @return array
     */
    public function actionTextChangePinYin()
    {
        $str = trim($this->request->post('str', ''));
        $space = trim($this->request->post('space', ""));
        $option = trim($this->request->post('option', 'none'));
        if (ToolStringService::mbStr($str) > 100000) return $this->errorResult(Config::$no);


        $result = CryptoService::textChangePinYin($str, $space, $option);
        return $this->endReponse('0',['pin_yin' => $result]);
    }

    /**
     * 中文字典
     * 喺尜鼕
     * @return array
     */
    public function actionTransformWithTone()
    {
        $str    = trim($this->request->post('str', ''));
        $space  = trim($this->request->post('space', ""));
        $type   = trim($this->request->post('type', 'dictionary'));
        $option = trim($this->request->post('option', 'unicode'));
        if (ToolStringService::mbStr($str) > 100000) return $this->errorResult(Config::$no);

        $result = CryptoService::transformWithTone($str, $space, $type, $option);
        return $this->endReponse('0',['pin_yin' => $result]);
    }

    /**
     * md5加密
     * @return array
     */
    public function actionMd5()
    {
        $str = trim($this->request->post('str', ''));
        if (ToolStringService::mbStr($str) > 100000) return $this->errorResult(Config::$no);

        return $this->endReponse('0',['md5_str' => md5($str)]);
    }

    /**
     * sha1加密
     * @return array|null
     */
    public function actionSha1()
    {
        $str = trim($this->request->post('str', ''));
        if (ToolStringService::mbStr($str) > 100000) return $this->errorResult(Config::$no);

        return $this->endReponse('0',['sha1_str' => sha1($str)]);
    }
}